//
//  RecordViewController.m
//  Yearly Plan
//
//  Created by admin on 2019/2/27.
//  Copyright © 2019年 admin. All rights reserved.
//

#import "RecordViewController.h"
#import "SPPageMenu.h"
#import "ChildRecordController1.h"
#import "ChildRecordController2.h"
#import "EidtInfoViewController.h"
@interface RecordViewController ()<SPPageMenuDelegate,UIScrollViewDelegate>

@property (nonatomic, weak) SPPageMenu *pageMenu;
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) NSMutableArray *myChildViewControllers;
@property(nonatomic,strong) NSMutableArray *dataArr;

@end

@implementation RecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Normal Event";
    
    UIImage *image = [UIImage imageNamed:@"xinjian"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithImage:image style:(UIBarButtonItemStyle)UIBarButtonItemStylePlain target:self action:@selector(pushToAddInfoVc)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    self.view.backgroundColor = HEX_COLOR(0xF1F1F1);
    self.dataArr = [NSMutableArray arrayWithObjects:@"Hang in the air", @"Completed", nil];
    [self createSegmentView:self.dataArr];
}

- (void)createSegmentView:(NSArray*)arr {
    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHeight(50)) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
    [pageMenu setItems:arr selectedItemIndex:0];
    pageMenu.backgroundColor = [UIColor whiteColor];
    pageMenu.itemTitleFont = RegularDefaultFont;
    //pageMenu.trackerWidth = kWidth(100);
    pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollEqualWidths;
    pageMenu.selectedItemTitleColor = HEX_COLOR(0xF7CE47);
    pageMenu.unSelectedItemTitleColor = [UIColor lightGrayColor];
    pageMenu.dividingLine.backgroundColor = RGBACOLOR(237, 237, 237, 1);
    pageMenu.dividingLineHeight =1.;
    [pageMenu.layer setShadowColor:HEX_COLOR(0xECECEC).CGColor];
    pageMenu.layer.shadowOffset = CGSizeMake(0, 2);
    pageMenu.layer.shadowOpacity = 0.4;
    pageMenu.delegate = self;
    pageMenu.tracker.backgroundColor = HEX_COLOR(0xF7CE47);
    [pageMenu setTrackerHeight:2 cornerRadius:2];
    [self.view addSubview:pageMenu];
    _pageMenu = pageMenu;
    self.pageMenu.bridgeScrollView = self.scrollView;
    [self setupView:self.pageMenu.selectedItemIndex];
}

-(UIScrollView *)scrollView
{
    if(!_scrollView){
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, pageMenuH + gap, SCREEN_WIDTH, scrollViewHeight)];//
        scrollView.delegate = self;
        scrollView.pagingEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.scrollsToTop = NO;
        [self.view addSubview:scrollView];
        [self.view sendSubviewToBack:scrollView];
        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * self.pageMenu.selectedItemIndex, 0);
        scrollView.contentSize = CGSizeMake(self.dataArr.count * SCREEN_WIDTH, 0);
        
        _scrollView = scrollView;
    }
    return _scrollView;
}

- (NSMutableArray *)myChildViewControllers {
    if (!_myChildViewControllers) {
        _myChildViewControllers = [NSMutableArray array];
        for (NSInteger index = 0; index < _dataArr.count; index++) {
            [_myChildViewControllers addObject:@1];
        }
    }
    return _myChildViewControllers;
}

#pragma mark - SPPageMenuDelegate
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if (labs(toIndex - fromIndex) >= 2) {
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:YES];
    } else {
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:YES];
    }
    if (self.myChildViewControllers.count <= toIndex) return;
    [self setupView:toIndex];
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index {
    [self setupView:index];
}

- (void)setupView:(NSInteger)index{
    id table = [self.myChildViewControllers objectAtIndex:index];
    if ([table isKindOfClass:[UIView class]]) {
        return;
    }
    switch (index) {
        case 0:
        {
            ChildRecordController2 *propertyVc = [ChildRecordController2 new];
            propertyVc.view.frame = CGRectMake(SCREEN_WIDTH * index, 0, SCREEN_WIDTH, self.scrollView.frame.size.height);
            [self.scrollView addSubview:propertyVc.view];
            [self addChildViewController:propertyVc];
            [self.myChildViewControllers replaceObjectAtIndex:index withObject:propertyVc.view];
        }
            break;
        case 1:
        {
            ChildRecordController1 *ChildVc = [ChildRecordController1 new];
            ChildVc.view.frame = CGRectMake(SCREEN_WIDTH * index, 0, SCREEN_WIDTH, self.scrollView.frame.size.height);
            [self.scrollView addSubview:ChildVc.view];
            [self addChildViewController:ChildVc];
            [self.myChildViewControllers replaceObjectAtIndex:index withObject:ChildVc.view];
        }
            break;
        default:
            break;
    }
}

-(void)pushToAddInfoVc {
    EidtInfoViewController *vc = [EidtInfoViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}



@end
